﻿using System;
using PTop.Code;
using PTop.Domain.Entity.Biz;
using PTop.Domain.IRepository.Biz;
using PTop.Repository.Biz;
using System.Collections.Generic;

namespace PTop.Application.Biz
{
    /// <summary>
    /// 用户统计App
    /// </summary>	
    public class UserStatisticsApp
    {
        private IUserStatisticsRepository service = new UserStatisticsRepository();

        public List<UserStatisticsEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<UserStatisticsEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                //expression = expression.And(t => t.F_OrgnaizeName.Contains(keyword));
                //expression = expression.Or(t => t.F_CreatorUserCode.Contains(keyword));
            }
            return service.FindList(expression, pagination);
        }
        public List<UserStatisticsEntity> GetListForHome()
        {
            var expression = ExtLinq.True<UserStatisticsEntity>();
            var pagination = new Pagination
            {
                page = 1,
                rows = 14,
                sidx = "F_Date desc",
                sord = "asc"
            };
            return service.FindList(expression, pagination);
        }
        public UserStatisticsEntity GetFormToday()
        {
            var today = DateTime.Now.Date;
            var entity = service.FindEntity(m => m.F_Date == today);
            if (entity == null)
            {
                var entityNew = new UserStatisticsEntity { F_Date = today };
                SubmitForm(entityNew, null);
                return entityNew;
            }
            return entity;
        }
        public UserStatisticsEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public void Delete(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }

        public void SubmitForm(UserStatisticsEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Modify(keyValue);
                service.Update(entity);
            }
            else
            {
                entity.Create();
                //entity.F_LastModifyTime = entity.F_CreatorTime;
                //entity.F_LastModifyUserId = entity.F_CreatorUserId;
                service.Insert(entity);
            }
        }

        /// <summary>
        /// 用户行为统计
        /// </summary>
        /// <param name="type"></param>
        public void AddCount(string type)
        {
            var entity = GetFormToday();
            switch (type)
            {
                case "F_ClickCount": entity.F_ClickCount++; break;
                case "F_CommentCount": entity.F_CommentCount++; break;
                case "F_CommentAuditCount": entity.F_CommentAuditCount++; break;
                case "F_SubscribeCount": entity.F_SubscribeCount++; break;
                case "F_ArticleCount": entity.F_ArticleCount++; break;
                case "F_MessageCount": entity.F_MessageCount++; break;
                case "F_MessageResponseCount": entity.F_MessageResponseCount++; break;
                case "F_JobCandidateCount": entity.F_JobCandidateCount++; break;
                case "F_JobDealCount": entity.F_JobDealCount++; break;
                case "F_LostPropertyCount": entity.F_LostPropertyCount++; break;
                case "F_QnAnswerCount": entity.F_QnAnswerCount++; break;
                default: return;
            }
            service.Update(entity, new[] { type });
        }
    }
}